home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0018_Stack Usage Report.pas < prev    next >
Pascal/Delphi Source File  |  1993-06-08  |  4KB  |  108 lines

  1. (*
  2. ===========================================================================
  3.  BBS: Canada Remote Systems
  4. Date: 05-30-93 (08:25)             Number: 8026
  5. From: STEVE ROGERS                 Refer#: NONE
  6.   To: PAUL HICKEY                   Recvd: NO  
  7. Subj: HELP PLEASE                    Conf: (1617) L-PASCAL
  8. ---------------------------------------------------------------------------
  9. PH> EP> {$M $A,B,C}
  10.  
  11. PH> I always set A and B to high and C to 0.  I want to allow the most memory I
  12. PH>can to program usage within the 640K limit.
  13.  
  14.   To get the most ram for your prog to use in the 640k set "B" to 0
  15.   and "C" to 655360. Setting "C" to 0 prevents you from accessing any
  16.   heap at all.
  17.  
  18.   "A" should be set to the amount of stack your program needs. I have a
  19.   unit that I use to help determine this. It was initially released for
  20.   TP4 but I've used with BP7 OK.
  21.  
  22. {***********************************************************
  23.   StackUse - A unit to report stack usage information
  24.  
  25.   by Richard S. Sadowsky
  26.   version 1.0 7/18/88
  27.   released to the public domain
  28.  
  29.   Inspired by a idea by Kim Kokkonen.
  30.  
  31.   This unit, when used in a Turbo Pascal 4.0 program, will
  32.   automatically report information about stack usage.  This is very
  33.   useful during program development.  The following information is
  34.   reported about the stack:
  35.  
  36.   total stack space
  37.   Unused stack space
  38.   Stack spaced used by your program
  39.  
  40.   The unit's initialization code handles three things, it figures out
  41.   the total stack space, it initializes the unused stack space to a
  42.   known value, and it sets up an ExitProc to automatically report the
  43.   stack usage at termination.  The total stack space is calculated by
  44.   adding 4 to the current stack pointer on entry into the unit.  This
  45.   works because on entry into a unit the only thing on the stack is the
  46.   2 word (4 bytes) far return value.  This is obviously version and
  47.   compiler specific.
  48.  
  49.   The ExitProc StackReport handles the math of calculating the used and
  50.   unused amount of stack space, and displays this information.  Note
  51.   that the original ExitProc (Sav_ExitProc) is restored immediately on
  52.   entry to StackReport.  This is a good idea in ExitProc in case a
  53.   runtime (or I/O) error occurs in your ExitProc!
  54.  
  55.   I hope you find this unit as useful as I have!
  56.  
  57. ************************************************************)
  58.  
  59. {$R-,S-} { we don't need no stinkin range or stack checking! }
  60. unit StackUse;
  61.  
  62. interface
  63.  
  64. var
  65.   Sav_ExitProc     : Pointer; { to save the previous ExitProc }
  66.   StartSPtr        : Word;    { holds the total stack size    }
  67.  
  68. implementation
  69.  
  70. {$F+} { this is an ExitProc so it must be compiled as far }
  71. procedure StackReport;
  72.  
  73. { This procedure may take a second or two to execute, especially }
  74. { if you have a large stack. The time is spent examining the     }
  75. { stack looking for our init value ($AA). }
  76.  
  77. var
  78.   I                : Word;
  79.  
  80. begin
  81.   ExitProc := Sav_ExitProc; { restore original exitProc first }
  82.  
  83.   I := 0;
  84.   { step through stack from bottom looking for $AA, stop when found }
  85.   while I < SPtr do
  86.     if Mem[SSeg:I] <> $AA then begin
  87.       { found $AA so report the stack usage info }
  88.       WriteLn('total stack space : ',StartSPtr);
  89.       WriteLn('unused stack space: ', I);
  90.       WriteLn('stack space used  : ',StartSPtr - I);
  91.       I := SPtr; { end the loop }
  92.     end
  93.     else
  94.       inc(I); { look in next byte }
  95. end;
  96. {$F-}
  97.  
  98.  
  99. begin
  100.   StartSPtr := SPtr + 4; { on entry into a unit, only the FAR return }
  101.                          { address has been pushed on the stack.     }
  102.                          { therefore adding 4 to SP gives us the     }
  103.                          { total stack size. }
  104.   FillChar(Mem[SSeg:0], SPtr - 20, $AA); { init the stack   }
  105.   Sav_ExitProc := ExitProc;              { save exitproc    }
  106.   ExitProc     := @StackReport;          { set our exitproc }
  107. end.
  108.